package com.koron.order.defect.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.change.dispatch.mapper.ChangeDefectDispatchMapper;
import com.koron.order.change.util.ThreadLocalOrderChange;
import com.koron.order.common.bean.base.OrderType;
import com.koron.order.defect.bean.po.DefectDispatch;
import com.koron.order.defect.mapper.DefectDispatchMapper;
import com.koron.order.defect.service.DefectDispatchService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (DefectDispatch)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-03 16:44:50
 */
@Service("defectDispatchService")
public class DefectDispatchServiceImpl implements DefectDispatchService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, DefectDispatch defectDispatch) {

        defectDispatch.setId(CodeTools.getCode32());
        defectDispatch.setCreateBy(getCurrUser().getAccount());
        defectDispatch.setCreateByName(getCurrUser().getName());
        defectDispatch.setCreateTime(new Date());

        int i = getDefectDispatchMapper(factory).insert(defectDispatch);
        return i;
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, DefectDispatch defectDispatch) {
        int i = getDefectDispatchMapper(factory).update(defectDispatch);
        return i;
    }

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, DefectDispatch defectDispatch) {

        return getDefectDispatchMapper(factory).insertOrUpdate(defectDispatch);

    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        int i = getDefectDispatchMapper(factory).deleteById(id);
        return i;
    }

    @Override
    @TaskAnnotation("queryById")
    public DefectDispatch queryById(SessionFactory factory, String id) {

        DefectDispatch defectDispatch = getDefectDispatchMapper(factory).queryById(id);

        return defectDispatch;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public DefectDispatch queryByOrderId(SessionFactory factory, String orderId) {

        DefectDispatch defectDispatch = getDefectDispatchMapper(factory).queryByOrderId(orderId);

        return defectDispatch;
    }

    @Override
    public int deleteByOrderId(SessionFactory factory, String orderId) {

        return getDefectDispatchMapper(factory).deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, DefectDispatch defectDispatch, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());

        List<DefectDispatch> list = getDefectDispatchMapper(factory).queryList(defectDispatch);

        return new PageInfo(list);
    }

    DefectDispatchMapper getDefectDispatchMapper(SessionFactory factory) {

        if (OrderType.CHANGE.equals(ThreadLocalOrderChange.get())) {
            return factory.getMapper(ChangeDefectDispatchMapper.class);
        }
        return factory.getMapper(DefectDispatchMapper.class);
    }

}